import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2024-12-11
 * Time: 23:27
 */
public class test2 {
    /*
    左括号入栈
     */

    public boolean isValid(String s) {
        // write code here
        if (s == null || s == "") {
            return true;
        }

        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(' || c == '{' || c == '[') {
                stack.push(c);
            } else {
                if (stack.isEmpty()){
                    return false;
                }

                char pop = stack.pop();
                if ((pop == '(' && c != ')')
                        || (pop == '{' && c != '}')
                        || (pop == '[' && c != ']')) {
                    return false;
                }
            }
        }

        return stack.isEmpty();
    }
}
